/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI.Dialogs;

import GUI.Gui;
import LN.Ln;
import Listeners.SelectKeyComboBoxListener;
import Objects.City;
import Objects.ItemPrintScreen;
import Objects.State;
import Objects.log_Personal;
import Objects.log_TPersonal;
import Tools.Datos;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.VBox;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author MITM
 */
public class Fxml_ImprimirController implements Initializable {

    @FXML 
    private AnchorPane ap_root; 
    
    @FXML 
    private Button bt_aceptar; 

    @FXML 
    private Button bt_cancelar; 
    
    @FXML
    private ComboBox<Object> cb_imp11;
    
    @FXML
    private ComboBox<Object> cb_imp21;
    
    @FXML
    private ComboBox<Object> cb_imp22;
    
    @FXML
    private ComboBox<Object> cb_imp31;
    
    @FXML
    private ImageView im_imp;

    @FXML
    private Label lb_imp11;

    @FXML
    private Label lb_imp21;

    @FXML
    private Label lb_imp22;

    @FXML
    private Label lb_imp31;

    @FXML
    private Label lb_b;

    @FXML 
    private Label lb_mensj; 

    @FXML
    private RadioButton rb_imp11;
    
    @FXML
    private RadioButton rb_imp12;
    
    @FXML
    private RadioButton rb_imp13;
    
    @FXML
    private TextField tf_buscar;

    @FXML
    private VBox vb_1;
    
    @FXML
    private VBox vb_2;
    
    @FXML
    private VBox vb_3;
    
    @FXML
    private VBox vb_b;
    

    private static final String path_rep = 
            java.util.ResourceBundle.getBundle("BD/DBcon").getString("path_rep");

    Map<String, Object> JrxmlParam = new HashMap<String, Object>();
    
    final ToggleGroup rb_group = new ToggleGroup();

    boolean keybackspace = false;

    /**
     * Initializes the controller class.
     * @param fxmlFileLocation
     * @param resources
     */     
    @Override // This method is called by the FXMLLoader when initialization is complete
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
        assert ap_root != null : "fx:id=\"ap_root\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'.";
        assert im_imp != null : "fx:id=\"im_imp\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'."; 
        assert lb_mensj != null : "fx:id=\"lb_mensj\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'."; 
        assert tf_buscar != null : "fx:id=\"tf_buscar\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'.";
        assert vb_1 != null : "fx:id=\"vb_1\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'.";
        assert vb_2 != null : "fx:id=\"vb_2\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'.";
        assert vb_3 != null : "fx:id=\"vb_3\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'.";
        assert vb_b != null : "fx:id=\"vb_b\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'.";
        
        vb_1.setVisible(false);
        vb_2.setVisible(false);
        vb_3.setVisible(false);
        vb_b.setVisible(false);
        
        // TODO        
        init_bt_aceptar();
        init_bt_cancelar();
        init_buttons(); //Establece los comportamientos de los botones
        init_screen_loads(Datos.getIdScreen());
    }

    /**
     * 
     */
    private void init_bt_aceptar(){
        assert bt_aceptar != null : "fx:id=\"bt_aceptar\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'.";

        /**
         * 
         */
        bt_aceptar.setOnMouseClicked((MouseEvent mouseEvent) -> {
            if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
                if(mouseEvent.getClickCount() > 0){
                    imprimir(Datos.getIdScreen());
                }
            }
        });
        /**
         * 
         */
        bt_aceptar.setOnKeyReleased((KeyEvent ke) -> {
            if (ke.getCode().equals(KeyCode.ENTER)){
                Datos.setRep_log_personal(Ln.getInstance().find_log_Personal(tf_buscar.getText()));
                imprimir(Datos.getIdScreen());
            }
        });
    }
    
    /**
     * 
     */
    private void init_bt_cancelar(){
        assert bt_cancelar != null : "fx:id=\"bt_cancelar\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'.";
         
        /**
         * 
         */
        bt_cancelar.setOnMouseClicked((MouseEvent mouseEvent) -> {
            if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
                if(mouseEvent.getClickCount() > 0){
                    closeDialog();
                }
            }
        });
        /**
         * 
         */
        bt_cancelar.setOnKeyReleased((KeyEvent ke) -> {
            if (ke.getCode().equals(KeyCode.ENTER)){
                closeDialog();
            }
        });
    }

    /**
     * 
     */
    public static void closeDialog(){ 
        Datos.setRep_log_personal(Ln.getInstance().load_log_Personal());
        Gui.getInstance().closeDialog();
    }

    /**
     * @param lb_mensj the lb_error to set
     */
    public void setLb_mensj(String lb_mensj) {
        this.lb_mensj.setText(lb_mensj);
    }    

    /**
     * @author MITM
     * @param idscreen
     */
    private void imprimir(int IdScreen){
        boolean result;

        JasperReport jReport = null;
        JasperViewer jview = null;
        JasperPrint jPrint = null;

        switch (IdScreen){
            case 1001001: // screen supplier
                break;
            case 2004000: // screen personal
                if((tf_buscar.getText() != null && !tf_buscar.equals("")) &&
                        (cb_imp11.getValue() == null ) && 
                        (cb_imp21.getValue() == null ) && 
                        (cb_imp31.getValue() == null ))
                {
                    Datos.setRep_log_personal(Ln.getInstance().find_log_Personal(tf_buscar.getText()));
                    JrxmlParam.put("p_subtitulo", "Buscar texto (Cédula o Nombres o Apellidos): " + tf_buscar.getText());
                }
                else if((tf_buscar.getText() == null) &&
                        (cb_imp11.getValue() == null) && 
                        (cb_imp21.getValue() == null) && 
                        (cb_imp31.getValue() == null))
                {
                    Datos.setRep_log_personal(Ln.getInstance().load_log_Personal());
                    JrxmlParam.put("p_subtitulo", "todo el personal");
                }
                else if((tf_buscar.getText() == null) &&
                        (cb_imp11.getValue() != null) && 
                        (cb_imp21.getValue() == null) && 
                        (cb_imp31.getValue() == null))
                {
                    String doc = null;
                    switch(cb_imp11.getSelectionModel().getSelectedItem().toString()){
                        case "Cert. de Salud":
                            doc = "cs";
                            break;
                        case "Cert. Medico":
                            doc = "cm";
                            break;
                        case "Lic. de Conducir":
                            doc = "lc";
                            break;
                        case "Man. de Alimento":
                            doc = "ma";
                            break;
                    }
                    Datos.setRep_log_personal(Ln.getInstance().print_log_Personal(doc, rb_group.getSelectedToggle().getUserData().toString()));
                    switch(rb_group.getSelectedToggle().getUserData().toString()){
                        case "xv":
                            JrxmlParam.put("p_subtitulo", cb_imp11.getSelectionModel().getSelectedItem().toString() + " - por vencer");
                            rb_imp11.setSelected(false);
                            break;
                        case "ve":
                            JrxmlParam.put("p_subtitulo", cb_imp11.getSelectionModel().getSelectedItem().toString() + " - vencido");
                            rb_imp12.setSelected(false);
                            break;
                        case "pe":
                            JrxmlParam.put("p_subtitulo", cb_imp11.getSelectionModel().getSelectedItem().toString() + " - pendiente");
                            rb_imp13.setSelected(false);
                            break;
                    }
                } 
                else if((tf_buscar.getText() == null) &&
                        (cb_imp11.getValue() == null) && 
                        (cb_imp21.getValue() != null) && 
                        (cb_imp31.getValue() == null))
                {
                    keybackspace = true;
                    JrxmlParam.put("p_subtitulo", "ubicación - " + cb_imp22.getValue());
                    switch(cb_imp21.getSelectionModel().getSelectedItem().toString()){
                        case "Ciudad":
                            City city = (City) cb_imp22.getValue();
                            Datos.setRep_log_personal(Ln.getInstance().print_log_Personal("ci", Integer.toString(city.getIdPoblacion())));
                            break;
                        case "Estado":
                            State state = (State) cb_imp22.getValue();            
                            Datos.setRep_log_personal(Ln.getInstance().print_log_Personal("es", Integer.toString(state.getIdPoblacion()) ));
                            break;
                    }
                    
                }
                else if((tf_buscar.getText() == null) &&
                        (cb_imp11.getValue() == null) && 
                        (cb_imp21.getValue() == null) && 
                        (cb_imp31.getValue() != null))
                {
                    keybackspace = true;
                    JrxmlParam.put("p_subtitulo", "Tipo de Personal ");
                    log_TPersonal log_tpersonal = (log_TPersonal) cb_imp31.getValue();            
                    Datos.setRep_log_personal(Ln.getInstance().print_log_Personal("tp", Integer.toString(log_tpersonal.getIdTPersonal())));
                }

                //Datos
                List<log_Personal> data = Ln.getList_log_Personal(Datos.getRep_log_personal());
                JRBeanCollectionDataSource JRDs = new JRBeanCollectionDataSource(data, true);

                //Parametros
                JrxmlParam.put("p_user", Datos.getSesion().getUsername());

                try{
                    jReport = (JasperReport) JRLoader.loadObject(path_rep + "/logistica/log_personal.jasper");
                    jPrint = JasperFillManager.fillReport(jReport, JrxmlParam, JRDs);
                    jview = new JasperViewer(jPrint, false);
                    jview.setTitle("DIGA - Listado de Personal (Logistica) ");

                } catch (JRException e){
                    Gui.getInstance().ventanaError("Error Cargando Reporte: \n"+e.getMessage()); 
                }
                
                tf_buscar.setText(null);
                cb_imp11.getSelectionModel().clearSelection();
                cb_imp21.getSelectionModel().clearSelection();
                cb_imp22.getSelectionModel().clearSelection();
                cb_imp31.getSelectionModel().clearSelection();
                break;
            case 2005000: // screen vehiculos
                break;
            case 8001000: // screen measure
                break;
            case 8002000: // screen groupsupplier
                break;
            case 9001001: // screen user
                break;
        }
        //jview.setIconImage(...);
        jview.setVisible(true);
        jview.setResizable(false);
        //Gui.getInstance().closeAuxDialog();                               
    }

    /**
     * Procedimiento que define los comportamientos en diversos Eventos 
     * de cada boton en la pantalla actual.
     */
    private void init_buttons(){
        /**
         * 
         */
        cb_imp11.setOnKeyPressed((KeyEvent ke) -> {
            if (ke.getCode().equals(KeyCode.BACK_SPACE)){
                cb_imp11.getSelectionModel().clearSelection();  
            }
        });
        /**
         * 
         */
        cb_imp21.setOnMousePressed((MouseEvent mouseEvent) -> {
            if(mouseEvent.getButton().equals(MouseButton.PRIMARY)){
                if(mouseEvent.getClickCount() > 0){
                    keybackspace = false;
                }
            }
        });
        /**
         * 
         */
        cb_imp21.setOnKeyPressed((KeyEvent ke) -> {
            if (ke.getCode().equals(KeyCode.BACK_SPACE)){
                keybackspace = true;
                cb_imp21.getSelectionModel().clearSelection();  
                cb_imp22.setItems(null);
            }
        });
        /**
         * metodo para mostrar el item (estado) seleccionado y cargar las ciudades
         * param: nombre del estado
         */
        cb_imp21.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends Object> ov, Object t, Object t1) -> {
            if(keybackspace == false){
                switch(t1.toString()){
                    case "Ciudad":
                        loadCity();
                        break;
                    case "Estado":
                        loadState();
                        break;
                }
            }
        });
        /**
         * 
         */
        cb_imp31.setOnKeyPressed((KeyEvent ke) -> {
            if (ke.getCode().equals(KeyCode.BACK_SPACE)){
                cb_imp31.getSelectionModel().clearSelection();  
            }
        });
        
    }
    /**
     * Procedimiento que define los comportamientos en diversos Eventos 
     * de cada boton en la pantalla actual.
     * @param IdScreen
     */
    private void init_screen_loads(int IdScreen){
        boolean result;

        switch (IdScreen){
            case 1001001: // screen supplier
                break;
            case 2004000: // screen personal
                assert lb_imp11 != null : "fx:id=\"lb_imp11\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'."; 
                assert rb_imp11 != null : "fx:id=\"rb_imp11\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'."; 
                assert rb_imp12 != null : "fx:id=\"rb_imp12\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'."; 
                assert rb_imp13 != null : "fx:id=\"rb_imp13\" was not injected: check your FXML file 'Fxml_Imprimir.fxml'."; 

                ItemPrintScreen[] itemprint;

                vb_1.setVisible(true);
                lb_imp11.setText("Documentos");

                rb_imp11.setToggleGroup(rb_group);
                rb_imp11.setText("Por Vencer");
                rb_imp11.setUserData("xv");

                rb_imp12.setToggleGroup(rb_group);
                rb_imp12.setText("Vencidos");
                rb_imp12.setUserData("ve");

                rb_imp13.setToggleGroup(rb_group);
                rb_imp13.setText("Pendientes");
                rb_imp13.setUserData("pe");

                itemprint = Ln.getInstance().find_PrintScreen(IdScreen, "vb_1");
                final ObservableList<Object> data11 = FXCollections.observableArrayList();
                data11.addAll(Arrays.asList(itemprint));          
                cb_imp11.setItems(data11); 
                new SelectKeyComboBoxListener(cb_imp11); 
                
                
                vb_2.setVisible(true);
                lb_imp21.setText("Ubicación:");
                itemprint = Ln.getInstance().find_PrintScreen(IdScreen, "vb_2");
                final ObservableList<Object> data21 = FXCollections.observableArrayList();
                data21.addAll(Arrays.asList(itemprint));          
                cb_imp21.setItems(data21); 
                new SelectKeyComboBoxListener(cb_imp21); 
                new SelectKeyComboBoxListener(cb_imp22); 

                
                vb_3.setVisible(true);
                lb_imp31.setText("T/Personal:");
                log_TPersonal[] log_tpersonal = Ln.getInstance().load_log_TPersonal();        
                final ObservableList<Object> data31 = FXCollections.observableArrayList();
                data31.addAll(Arrays.asList(log_tpersonal));          
                cb_imp31.setItems(data31); 
                new SelectKeyComboBoxListener(cb_imp31); 

                
                vb_b.setVisible(true);
                lb_b.setText("Buscar texto (Cédula o Nombres o Apellidos):");
                tf_buscar.setText(null);
                
                break;
            case 2005000: // screen vehiculos
                break;
            case 8001000: // screen measure
                break;
            case 8002000: // screen groupsupplier
                break;
            case 9001001: // screen user
                break;
        }
    }

    /**
     * Procedimiento que obtiene los Distintos Estados de la BD
     * y los carga en el COMBOBOX
     * param: nombre del pais
     */
    private void loadState(){ 
        State[] state = Ln.getInstance().load_State();
        final ObservableList<Object> data = FXCollections.observableArrayList();
        data.addAll(Arrays.asList(state));          
        cb_imp22.setItems(data); 
    }    
    /**
     * Procedimiento que obtiene las Distintas Ciudades de la BD
     * y los carga en el COMBOBOX
     * param: nombre del estado
     */
    private void loadCity(){        
        City[] city = Ln.getInstance().load_City();
        final ObservableList<Object> data = FXCollections.observableArrayList();
        data.addAll(Arrays.asList(city));          
        cb_imp22.setItems(data); 
    }    

    
}
